﻿using System;

namespace ShapesExample
{
    // 定义形状接口  
    public interface IShape
    {
        double Area();  // 计算面积  
        bool IsValid(); // 判断形状是否合法  
    }

    // 长方形类  
    public class Rectangle : IShape
    {
        public double Width { get; }
        public double Height { get; }

        public Rectangle(double width, double height)
        {
            Width = width;
            Height = height;
        }

        public double Area()
        {
            return Width * Height; // 面积 = 宽 * 高  
        }

        public bool IsValid()
        {
            return Width > 0 && Height > 0; // 合法性检查: 宽和高都必须大于0  
        }
    }

    // 正方形类，正方形是长方形的特例  
    public class Square : Rectangle
    {
        public Square(double side) : base(side, side) // 调用基类的构造函数  
        {
        }
    }

    // 三角形类  
    public class Triangle : IShape
    {
        public double Base { get; }
        public double Height { get; }
        public double SideA { get; }
        public double SideB { get; }
        public double SideC { get; }

        public Triangle(double baseLength, double height, double sideA, double sideB, double sideC)
        {
            Base = baseLength;
            Height = height;
            SideA = sideA;
            SideB = sideB;
            SideC = sideC;
        }

        public double Area()
        {
            return 0.5 * Base * Height; // 面积 = 0.5 * 底 * 高  
        }

        public bool IsValid()
        {
            // 判断三角形合法性（任意两边之和大于第三边）  
            return SideA + SideB > SideC && SideA + SideC > SideB && SideB + SideC > SideA &&
                   Base > 0 && Height > 0; // 还需确保底和高大于0  
        }
    }

    // 测试程序  
    class Program
    {
        static void Main(string[] args)
        {
            // 创建形状实例  
            IShape rectangle = new Rectangle(10, 5);
            IShape square = new Square(4);
            IShape triangle = new Triangle(6, 4, 5, 5, 6);

            // 输出形状的面积和合法性  
            DisplayShapeInfo(rectangle);
            DisplayShapeInfo(square);
            DisplayShapeInfo(triangle);
        }

        // 显示形状信息  
        static void DisplayShapeInfo(IShape shape)
        {
            Console.WriteLine($"形状合法性: {shape.IsValid()}");
            Console.WriteLine($"面积: {shape.Area()}");
            Console.WriteLine();
        }
    }
}